#include <iostream>
#include <fstream>
#include <sstream>

#include "SequenceList.h"
#include "LinkedList.h"

int main(int argc, char* argv[])
{
    if (argc != 1) {
        std::cerr << "Usage: Lab1 filename.ext\n";
    }

    std::string filename = "Lab1test.DAT";

    std::ifstream file(filename);

    if (!file) {
        std::cerr << "Cannot open file: " << filename << std::endl;
        throw std::exception("Failed to access file.");
    }

    SequenceList seqList;
    LinkedList linkedList;
    int insertIndex = 0;

    std::string line;
    while (file >> line) {
        std::cout << line << std::endl;
        
        StudentInfo studentInfo(line);
        seqList.Insert(insertIndex, studentInfo);
        linkedList.InsertFront(studentInfo);
        insertIndex++;
    }

    std::string choice;
    bool running = true;
    while (running)
    {
        std::cout << "Testing SequenceList now. \nWhat do you want to do? ";
        std::cin >> choice;

        char operation = choice.at(0);
        std::istringstream indexStream(choice.substr(1));
        int index = -1; // -1 represents undefined
        indexStream >> index;
        int comma1 = choice.find(',');
        bool hasStudentInfo = comma1 != -1;

        switch (toupper(operation))
        {
        case 'P':
            if (index == -1)
            {
                seqList.PrintAll();
            }
            else
            {
                seqList.PrintWhich(index);
            }
            break;
        case 'I':
            if (!hasStudentInfo)
            {
                throw std::exception("No student info");
            }
            else
            {
                std::string line = choice.substr(comma1 + 1);
                StudentInfo studentInfo(line);
                seqList.Insert(index - 1, studentInfo);
            }
            break;
        case 'D':
            seqList.DeleteWhich(index);
            break;
        case 'Q':
            running = false;
            break;
        default:
            break;
        }
    }

    running = true;
    while (running)
    {
        std::cout << "Testing LinkedList now. \nWhat do you want to do? ";
        std::cin >> choice;

        char operation = choice.at(0);
        std::istringstream indexStream(choice.substr(1));
        int index = -1; // -1 represents undefined
        indexStream >> index;
        int comma1 = choice.find(',');
        bool hasStudentInfo = comma1 != -1;

        switch (toupper(operation))
        {
        case 'P':
            if (index == -1)
            {
                linkedList.PrintAll();
            }
            else
            {
                linkedList.Print(index);
            }
            break;
        case 'I':
            if (!hasStudentInfo)
            {
                throw std::exception("No student info");
            }
            else
            {
                std::string line = choice.substr(comma1 + 1);
                StudentInfo studentInfo(line);
                linkedList.Insert(index, studentInfo);
            }
            break;
        case 'D':
            linkedList.Delete(index);
            break;
        case 'Q':
            running = false;
            break;
        default:
            break;
        }
    }

    std::cout << "Good bye!" << std::endl;


    std::cin.get();
}
